Lab 23

  1. Write a complete C++ program that does the following:
  2. Example main function that uses insertionSort:
    int main() { int cap = 10; int nums[cap]; fillArray(nums, cap); // fill array with random numbers printArray(nums, cap); // print array insertionSort(nums, cap); // sort elements in array printArray(nums, cap); // print sorted array return 0; }

    Sample run of program:
    89 16 58 15 93 36 55 33 12 24 12 15 16 24 33 36 55 58 89 93
  3. Write a complete C++ program that does the following:
  4. Example main function that uses these functions:
    int main() { int nums[10]; double average; cout << "Enter ten numbers:\n"; // read in numbers from user readInput(nums, 10); // compute average of elements in array average = arrayAverage(nums, 10); // print average value to monitor cout << average << endl; // print array elemnets that are greater than average greaterThanAverage(nums, 10, average); return 0; }

    Sample run of program>
    Enter ten numbers: 4 1 7 3 8 4 6 9 2 1 4.5 7 8 6 9
  5. Eight queens are to be placed on an 8 x 8 chessboard in such a way that one queen is to be placed in each row. Write a complete C++ program that does the following:
  6. Example main function that uses these functions:
    int main() { int queenColumns[8]; cout << "Enter columns for queens:\n"; readInput(queenColumns, 8); printBoard(queenColumns, 8); return 0; }

    Sample run of program:
    Enter columns for queens: 3 7 1 6 2 5 4 6 ...Q.... .......Q .Q...... ......Q. ..Q..... .....Q.. ....Q... ......Q.
  7. (Similar to the problem above, but the user specifies a row for each column instead of a column for each row.) Eight queens are to be placed on a 8 x 8 chessboard in such a way that one queen is to be placed in each column. Write a complete C++ program that does the following:
  8. Example main function that uses these functions:
    int main() { int queenRows[8]; cout << "Enter rows for queens:\n"; readInput(queenRows, 8); printBoard(queenRows, 8); return 0; }

    Sample run of program:
    Enter rows for queens: 6 2 4 7 1 0 3 5 .....Q.. ....Q... .Q...... ......Q. ..Q..... .......Q Q....... ...Q....
  9. Write a complete C++ program that does the following:
  10. Example main function that uses fillWithDistinctNums:
    int main() { int nums[20]; fillWithDistinctNums(nums, 20); printArray(nums, 20); return 0; }

    Sample run of program. Notice that no number is repeated:
    4 20 9 3 1 12 7 16 11 19 8 18 15 2 13 5 14 6 10 17

    Another sample run of program:
    6 15 14 17 3 13 16 5 12 10 2 8 4 18 19 9 11 1 7 20

    For a more concrete set of hints to solve this problem, here is an outline of the code needed for fillWithDistinctNumbers:
    fillWithDistinctNums (int nums[], int cap): // need srand to guarantee distinct sequence of random numbers for each run srand(time(0)); for loop to access all elements of input array at index i: declare bool variable numOK and initialize to false while !numOK: set nums[i] = random number between 1 and 20 set numOK equal to true // need to loop through existing array entries (all entries to left of current index) // to see if new num is a duplicate of any of them for loop checking indices 0 through i - 1 (int j = 0; j < i; j++): // check if nums[i] == nums[j] // if this is true, the new number is not OK if nums[i] == nums[j] set numOK = false